package com.zuo.mall.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zuo.mall.common.api.CommonPage;
import com.zuo.mall.common.api.CommonResult;
import com.zuo.mall.dto.PmsProductParam;
import com.zuo.mall.dto.PmsProductQueryParam;
import com.zuo.mall.dto.PmsProductResult;
import com.zuo.mall.entity.PmsProduct;
import com.zuo.mall.service.PmsProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("商品操作")
@Tag(name = "pmsProductController", description = "商品管理")
@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    private PmsProductService productService;


    @PostMapping("create")
    public CommonResult create(@RequestBody PmsProductParam pmsProductParam) {
        int count = productService.create(pmsProductParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @GetMapping("/list")
    @ApiOperation("查询商品")
    public CommonResult<CommonPage<PmsProduct>> getList(PmsProductQueryParam productQueryParam
    ) {
        CommonPage<PmsProduct> pmsProductList = productService.list(productQueryParam);

        return CommonResult.success(pmsProductList);
    }


    @PostMapping("update/publishStatus")
    public CommonResult updatePublishStatus(@RequestParam List<Long> ids, @RequestParam Integer publishStatus) {
        int count = productService.updatePublishStatusBatch(ids, publishStatus);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @PostMapping("update/recommendStatus")
    public CommonResult updateRecommendStatus(@RequestParam List<Long> ids, @RequestParam Integer recommendStatus) {
        int count = productService.updateRecommendStatusBatch(ids, recommendStatus);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @PostMapping("update/newStatus")
    public CommonResult updateNewStatus(@RequestParam List<Long> ids, @RequestParam Integer newStatus) {
        int count = productService.updateNewStatusBatch(ids, newStatus);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @PostMapping("update/deleteStatus")
    public CommonResult updateDeleteStatus(@RequestParam List<Long> ids, @RequestParam Integer deleteStatus) {
        int count = productService.updateDeleteStatusBatch(ids, deleteStatus);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @GetMapping("updateInfo/{id}")
    public CommonResult<PmsProductResult> getUpdateInfo(@PathVariable("id") Long id) {
        PmsProductResult result = productService.getUpdateInfo(id);
        return CommonResult.success(result);
    }

    @ApiOperation("更新商品")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProductParam productParam) {
        int count = productService.update(id, productParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

}
